from django.template import resolve_variable, Library

register = Library()

@register.filter
def cookies_off(value, arg):
    if value != 'step1':
        from lp.lpx.myusers import is_anonymous
        return is_anonymous(arg)
    return False

@register.filter
def list_nest(value, arg):
    return value[arg]

@register.filter
def input_size(value, arg):
    try:
        if len(value) < int(arg):
            return arg
        else:
            return len(value)
    except:
        return arg

@register.filter
def obj_func_width(value, arg):
    w = int(value)*8
    return str(w)

@register.filter
def const_width(value, arg):
    w = int(value)*8+10
    return str(w)

@register.filter
def const_height(value):
    h = int(value)
    return str(h)

@register.inclusion_tag('paginator.html', takes_context=True)
def Paginator(context):
    page = context['page']
    max_page = context['max_page']
    return {
        'is_paginated'  :   page.paginator.num_pages > 1,
        'has_next'      :   page.has_next(),
        'has_previous'  :   page.has_previous(),
        'next'          :   page.next_page_number(),
        'previous'      :   page.previous_page_number(),
        'page'          :   page.number,
        'pages'         :   page.paginator.num_pages,
        'hits'          :   page.paginator.count,
        'max_page'      :   max_page,
    }
